import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:lmmi/common/services/user_service.dart';
import 'package:lmmi/common/entity/user.dart';

void main() {
  group('Delete Account Tests', () {
    setUpAll(() async {
      // 初始化 Hive 测试环境
      Hive.init('./test/hive_delete_test_db');

      // 注册适配器
      if (!Hive.isAdapterRegistered(0)) {
        Hive.registerAdapter(UserEntityAdapter());
      }

      // 打开用户数据盒子
      await Hive.openBox<UserEntity>('users');
      await Hive.openBox('current_user');
    });

    tearDownAll(() async {
      // 清理测试数据
      await Hive.deleteFromDisk();
    });

    setUp(() async {
      // 每个测试前清空数据
      final userBox = Hive.box<UserEntity>('users');
      final currentUserBox = Hive.box('current_user');
      await userBox.clear();
      await currentUserBox.clear();
    });

    test('应该能够成功删除用户账号', () async {
      // 创建测试用户
      final testUser = UserEntity(
        id: 'test_user_123',
        username: 'testuser',
        password: 'password123',
        avatar: 'test_avatar.png',
        share: [],
        isVip: false,
        like: [],
        reportedPosts: [],
        reportedUsers: [],
        reportedChats: [],
      );

      // 注册用户
      final userBox = Hive.box<UserEntity>('users');
      await userBox.put(testUser.username, testUser);

      // 设置为当前用户
      final currentUserBox = Hive.box('current_user');
      await currentUserBox.put('user', testUser.toJson());

      // 验证用户存在
      expect(userBox.containsKey('testuser'), true);
      expect(UserService.getCurrentUser()?.id, 'test_user_123');

      // 删除账号
      final success = await UserService.deleteAccount('test_user_123');

      // 验证删除成功
      expect(success, true);
      expect(userBox.containsKey('testuser'), false);
      expect(UserService.getCurrentUser(), null);
    });

    test('删除不存在的用户应该返回false', () async {
      // 尝试删除不存在的用户
      final success = await UserService.deleteAccount('non_existent_user');

      // 验证删除失败
      expect(success, false);
    });

    test('删除其他用户不应该影响当前用户状态', () async {
      // 创建两个测试用户
      final currentUser = UserEntity(
        id: 'current_user_123',
        username: 'current',
        password: 'password123',
        avatar: 'current_avatar.png',
        share: [],
        isVip: false,
        like: [],
        reportedPosts: [],
        reportedUsers: [],
        reportedChats: [],
      );

      final otherUser = UserEntity(
        id: 'other_user_123',
        username: 'other',
        password: 'password123',
        avatar: 'other_avatar.png',
        share: [],
        isVip: false,
        like: [],
        reportedPosts: [],
        reportedUsers: [],
        reportedChats: [],
      );

      // 注册两个用户
      final userBox = Hive.box<UserEntity>('users');
      await userBox.put(currentUser.username, currentUser);
      await userBox.put(otherUser.username, otherUser);

      // 设置当前用户
      final currentUserBox = Hive.box('current_user');
      await currentUserBox.put('user', currentUser.toJson());

      // 删除其他用户
      final success = await UserService.deleteAccount('other_user_123');

      // 验证删除成功，但当前用户状态不变
      expect(success, true);
      expect(userBox.containsKey('other'), false);
      expect(userBox.containsKey('current'), true);
      expect(UserService.getCurrentUser()?.id, 'current_user_123');
    });
  });
}
